
// Our class to dynamically load libraries!

#ifndef _CLIBRARY_HPP
#define _CLIBRARY_HPP

#include <windows.h>

class CLibrary  {

protected:
	HINSTANCE handle;

public:

	// Constructors
	CLibrary() : handle(HINSTANCE(0)) {}

	CLibrary(LPTSTR libname) : handle(HINSTANCE(0))
   {
		Open(libname);
	}

	// To open/reopen a DLL
	bool Open(LPTSTR libname)
   {
		Close();
		handle = ::LoadLibrary(libname);
		return IsOpen();
	}

	// To get the address of a procedure
	FARPROC GetProcAddress(LPTSTR libname) const
   {
		return ::GetProcAddress(handle,libname);
	}

	FARPROC operator() (LPTSTR libname) const
   {
		return ::GetProcAddress(handle,libname);
	}

	// To query if DLL was successfully loaded
	bool IsOpen() const
   {
		return (handle >= (HINSTANCE)32);
	}

	// To close the library
	void Close()
   {
		if (IsOpen())
      {
			::FreeLibrary(handle);
		}
		handle = (HINSTANCE)0;
	}
	
	// Destructor
	~CLibrary() {
		Close();
	}
};

#endif